function varargout = Medidor_Velocidade_Som(varargin)
% MEDIDOR_VELOCIDADE_SOM MATLAB code for Medidor_Velocidade_Som.fig
%      MEDIDOR_VELOCIDADE_SOM, by itself, creates a new MEDIDOR_VELOCIDADE_SOM or raises the existing
%      singleton*.
%
%      H = MEDIDOR_VELOCIDADE_SOM returns the handle to a new MEDIDOR_VELOCIDADE_SOM or the handle to
%      the existing singleton*.
%
%      MEDIDOR_VELOCIDADE_SOM('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in MEDIDOR_VELOCIDADE_SOM.M with the given input arguments.
%
%      MEDIDOR_VELOCIDADE_SOM('Property','Value',...) creates a new MEDIDOR_VELOCIDADE_SOM or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before Medidor_Velocidade_Som_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to Medidor_Velocidade_Som_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help Medidor_Velocidade_Som

% Last Modified by GUIDE v2.5 06-Sep-2013 12:59:50

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @Medidor_Velocidade_Som_OpeningFcn, ...
                   'gui_OutputFcn',  @Medidor_Velocidade_Som_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before Medidor_Velocidade_Som is made visible.
function Medidor_Velocidade_Som_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to Medidor_Velocidade_Som (see VARARGIN)

% Dados

addpath('../US')

try
    load -mat UserConfig
    handles.concentracoes_vec{1,1}=-1;
    handles.UserData=UserData;
    foi=SetValues(handles);
catch
    handles.UserData.FileName=0;
    handles.UserData.FileDir=0;
end
% Choose default command line output for Medidor_Velocidade_Som
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);

% UIWAIT makes Medidor_Velocidade_Som wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = Medidor_Velocidade_Som_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;



function inicio_1_Callback(hObject, eventdata, handles)
% hObject    handle to inicio_1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
str = get(hObject, 'String');
handles.UserData.inicio_pi_1=str2double(str);
guidata(hObject, handles);

% Hints: get(hObject,'String') returns contents of inicio_1 as text
%        str2double(get(hObject,'String')) returns contents of inicio_1 as a double


% --- Executes during object creation, after setting all properties.
function inicio_1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to inicio_1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function fim_1_Callback(hObject, eventdata, handles)
% hObject    handle to fim_1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
str = get(hObject, 'String');
handles.UserData.fim_pi_1=str2double(str);
guidata(hObject, handles);

% Hints: get(hObject,'String') returns contents of fim_1 as text
%        str2double(get(hObject,'String')) returns contents of fim_1 as a double



% --- Executes during object creation, after setting all properties.
function fim_1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to fim_1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function fim_2_Callback(hObject, eventdata, handles)
% hObject    handle to fim_2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
str = get(hObject, 'String');
handles.fim_pi_2=str2double(str);
guidata(hObject, handles);

% Hints: get(hObject,'String') returns contents of fim_2 as text
%        str2double(get(hObject,'String')) returns contents of fim_2 as a double


% --- Executes during object creation, after setting all properties.
function fim_2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to fim_2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function inicio_2_Callback(hObject, eventdata, handles)
% hObject    handle to inicio_2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
str = get(hObject, 'String');
handles.UserData.inicio_pi_2=str2double(str);
guidata(hObject, handles);

% Hints: get(hObject,'String') returns contents of inicio_2 as text
%        str2double(get(hObject,'String')) returns contents of inicio_2 as a double


% --- Executes during object creation, after setting all properties.
function inicio_2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to inicio_2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in caclula_pushbutton.
function caclula_pushbutton_Callback(hObject, eventdata, handles)
% hObject    handle to caclula_pushbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

[a tam]=size(handles.arquivo);
for b=1:tam
file=handles.arquivo{1, b};  
clear dados
clear tt1

clear dist
clear distpercorrida
clear velocidades
file
dados=double(US_lerDados(file, handles.UserData.amostras_por_onda, 0));

for i=1:size(dados, 2)
   %i=590;
    [tt1(i) dist(i)]=US_tt(dados(:,i),handles.UserData.freq_transdutor, 3 , handles.UserData.freq_amostragem, ...
                           handles.UserData.inicio_pi_1:handles.UserData.fim_pi_1, ...
                           handles.UserData.inicio_pi_2:handles.fim_pi_2, 1480);
    %break;
end


distpercorrida(1, 1:size(tt1,2))= 2*handles.UserData.distancia;
velocidades=distpercorrida./tt1;
handles.media_dp(1,b)=mean(velocidades');
handles.media_dp(2,b)=std(velocidades');

end
indice=find([handles.concentracoes_vec{1,:}]==handles.concentracao)-1;
handles.media_dp(1, tam+1)=mean(handles.media_dp(1, 1:tam));
handles.media_dp(2, tam+1)=mean(handles.media_dp(2, 1:tam));
errorbar(handles.axes2, handles.concentracao, handles.media_dp(1, tam+1), handles.media_dp(2, tam+1), 'r*');
handles.dados_export(: ,:, indice)=handles.media_dp;
guidata(hObject, handles);




function edit_amostras_por_onda_Callback(hObject, eventdata, handles)
% hObject    handle to edit_amostras_por_onda (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

str = get(hObject, 'String');
handles.UserData.amostras_por_onda=str2double(str);
guidata(hObject, handles);


% Hints: get(hObject,'String') returns contents of edit_amostras_por_onda as text
%        str2double(get(hObject,'String')) returns contents of edit_amostras_por_onda as a double


% --- Executes during object creation, after setting all properties.
function edit_amostras_por_onda_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_amostras_por_onda (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit_freq_amostragem_Callback(hObject, eventdata, handles)
% hObject    handle to edit_freq_amostragem (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

str = get(hObject, 'String');
handles.UserData.freq_amostragem=str2num(str)*10^6;
guidata(hObject, handles);



% Hints: get(hObject,'String') returns contents of edit_freq_amostragem as text
%        str2double(get(hObject,'String')) returns contents of edit_freq_amostragem as a double


% --- Executes during object creation, after setting all properties.
function edit_freq_amostragem_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_freq_amostragem (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end




function edit_freq_transdutor_Callback(hObject, eventdata, handles)
% hObject    handle to edit_freq_transdutor (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

str = get(hObject, 'String');
handles.UserData.freq_transdutor=str2num(str)*10^6;
guidata(hObject, handles);



% Hints: get(hObject,'String') returns contents of edit_freq_transdutor as text
%        str2double(get(hObject,'String')) returns contents of edit_freq_transdutor as a double


% --- Executes during object creation, after setting all properties.
function edit_freq_transdutor_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_freq_transdutor (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.

if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in dados_pushbutton.
function dados_pushbutton_Callback(hObject, eventdata, handles)
% hObject    handle to dados_pushbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB

for i=1:size(handles.arquivo, 2)
handles.dados{1,i}=double(US_lerDados(handles.arquivo{1,i}, handles.UserData.amostras_por_onda, 0));
end
pulsos=num2str(size(handles.dados{1,1}, 2));
(size(handles.dados{1,1}, 2));
h = uicontrol(handles.uipanel2,'Style','text',...
                'String', pulsos,...
                'Position',[76 96 30 15]);
[a, b]=size(handles.concentracoes_vec);
if isempty(find([handles.concentracoes_vec{1,:}]==handles.concentracao))
    handles.concentracoes_vec{1,b+1}=handles.concentracao;
end
guidata(hObject, handles);

% handles    structure with handles and user data (see GUIDATA)



function intervalo_plot_inicio_Callback(hObject, eventdata, handles)
% hObject    handle to intervalo_plot_inicio (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
 str = get(hObject, 'String');
 handles.UserData.inicio_plot=str2double(str);
 guidata(hObject, handles);
% Hints: get(hObject,'String') returns contents of intervalo_plot_inicio as text
%        str2double(get(hObject,'String')) returns contents of intervalo_plot_inicio as a double


% --- Executes during object creation, after setting all properties.
function intervalo_plot_inicio_CreateFcn(hObject, eventdata, handles)
% hObject    handle to intervalo_plot_inicio (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function intervalo_plot_fim_Callback(hObject, ~, handles)
% hObject    handle to intervalo_plot_fim (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

str = get(hObject, 'String');
handles.UserData.fim_plot=str2double(str);
guidata(hObject, handles);

% Hints: get(hObject,'String') returns contents of intervalo_plot_fim as text
%        str2double(get(hObject,'String')) returns contents of intervalo_plot_fim as a double


% --- Executes during object creation, after setting all properties.
function intervalo_plot_fim_CreateFcn(hObject, eventdata, handles)
% hObject    handle to intervalo_plot_fim (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in plotar_pushbutton.
function plotar_pushbutton_Callback(hObject, eventdata, handles)
% hObject    handle to plotar_pushbutton (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
plot(handles.axes1, handles.dados{1,1}(:,handles.UserData.inicio_plot:handles.UserData.fim_plot));
set(handles.axes1,'XGrid','on','YGrid','on') 
guidata(hObject, handles);



function edit_concentracao_Callback(hObject, eventdata, handles)
% hObject    handle to edit_concentracao (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
str = get(hObject, 'String');
handles.concentracao=str2num(str);
guidata(hObject, handles);
% Hints: get(hObject,'String') returns contents of edit_concentracao as text
%        str2double(get(hObject,'String')) returns contents of edit_concentracao as a double


% --- Executes during object creation, after setting all properties.
function edit_concentracao_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_concentracao (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function edit_arquivo_Callback(hObject, eventdata, handles)
% hObject    handle to edit_arquivo (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
str = get(hObject, 'String');
handles.UserData.arquivo=str;
guidata(hObject, handles);
% Hints: get(hObject,'String') returns contents of edit_arquivo as text
%        str2double(get(hObject,'String')) returns contents of edit_arquivo as a double


% --- Executes during object creation, after setting all properties.
function edit_arquivo_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_arquivo (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in pushbutton_arquivo.
function pushbutton_arquivo_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_arquivo (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


[CurrentFileName CurrentFileDir]=uigetfile(['' handles.UserData.FileDir '.bin'], 'Escolha os arquivos referentes a uma concentracao','MultiSelect', 'on' );

if CurrentFileDir ~= 0
    if iscell(CurrentFileName)==0
    a=CurrentFileName;
    clear CurrentFileName
    CurrentFileName{1,1}=a;
    clear a
    end
    
    handles.UserData.FileName = CurrentFileName{1,1};   
    handles.UserData.FileDir = CurrentFileDir;
end
set(handles.edit_arquivo,'String', ['' handles.UserData.FileDir '' handles.UserData.FileName '']);

if isfield(handles, 'arquivo')
handles=rmfield(handles,'arquivo');
end
[a b]=size(CurrentFileName);
for i=1:b
handles.arquivo{1, i}= ['' handles.UserData.FileDir '' CurrentFileName{1,i}  ''];
end
guidata(hObject,handles);

    


% --- Executes when user attempts to close figure1.
function figure1_CloseRequestFcn(hObject, eventdata, handles)
% hObject    handle to figure1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
clear UserData
UserData=handles.UserData;
try
save UserConfig.mat UserData 
catch
end
% Hint: delete(hObject) closes the figure
delete(hObject);

function values=SetValues(handles)
values=1;
try
    set(handles.edit_amostras_por_onda, 'String', num2str(handles.UserData.amostras_por_onda));
catch
end

try
    set(handles.edit_freq_amostragem, 'String', num2str(handles.UserData.freq_amostragem/(10^6)));
catch
end

try
    set(handles.edit_freq_transdutor, 'String', num2str(handles.UserData.freq_transdutor/(10^6)));
catch
end

try
    set(handles.edit_arquivo, 'String', handles.UserData.file);
catch
end

try
    set(handles.intervalo_plot_inicio, 'String', num2str(handles.UserData.inicio_plot));
catch
end

try
    set(handles.intervalo_plot_fim, 'String', num2str(handles.UserData.fim_plot));
catch
end

try
    set(handles.inicio_1, 'String', num2str(handles.UserData.inicio_pi_1));
catch
end

try
    set(handles.inicio_2, 'String', num2str(handles.UserData.inicio_pi_2));
catch
end

try
    set(handles.fim_1, 'String', num2str(handles.UserData.fim_pi_1));
catch
end
 
 try
     set(handles.edit_distancia, 'String', num2str(handles.UserData.distancia*1e3));
 catch
 end


% --- Executes on button press in pushbutton_save.
function pushbutton_save_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_save (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

[tam1 tam2 tam3]=size(handles.dados_export);
alpha='ABCDEFGHIJKLMNOPQRSTUVWXYZ';
letra=alpha(tam2);
letra_totais=alpha(tam2+2);
[SaveName SaveDir]=uiputfile('.xlsx');
xlswrite(['' SaveDir '' SaveName ''], {'Medias Totais'}, 1 ,['' letra_totais '1']); 

for i=1:tam3
start=3*i-1;
xlswrite(['' SaveDir '' SaveName ''], {['Concentracao ' num2str(handles.concentracoes_vec{1,i+1}) '']}, 1, ['A' num2str(start)]); 
xlswrite(['' SaveDir '' SaveName ''], {'Vel. Media (m/s)'}, 1 ,['A' num2str(start+1) '']); 
xlswrite(['' SaveDir '' SaveName ''], {'Desvio Padrao'},  1, ['A' num2str(start+2) '']); 
xlswrite(['' SaveDir '' SaveName ''], handles.dados_export(:, 1:tam2-1, i), ['B' num2str(start+1) ':' letra '' num2str(start+2) ' ']);  
xlswrite(['' SaveDir '' SaveName ''], handles.dados_export(:, tam2, i), ['' letra_totais '' num2str(start+1) ':' letra_totais '' num2str(start+2) ' ']); 
end


msg='O arquivo foi salvo com sucesso'



function edit_distancia_Callback(hObject, eventdata, handles)
% hObject    handle to edit_distancia (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
str = get(hObject, 'String');
handles.UserData.distancia=str2num(str)*1e-3;
guidata(hObject, handles);
% Hints: get(hObject,'String') returns contents of edit_distancia as text
%        str2double(get(hObject,'String')) returns contents of edit_distancia as a double


% --- Executes during object creation, after setting all properties.
function edit_distancia_CreateFcn(hObject, eventdata, handles)
% hObject    handle to edit_distancia (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
